# coding:utf-8
from django.db import models
from django.urls import reverse
# Create your models here.

# 学校表
class Schools(models.Model):
    name = models.CharField('学校名称',max_length=128, unique=True)
    school_id = models.CharField('编号',max_length=128, unique=True)
    content = models.CharField('简介',max_length=256)
    deleted = models.BooleanField(default=False)

    class Meta:
        db_table = 'schools'
        verbose_name = '学校'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name


# 教师表
class Teachers(models.Model):
    gender = (('male', '男'), ('female', '女'))
    name = models.CharField('姓名',max_length=128)
    teacher_id = models.CharField('编号',max_length=128, unique=True)
    sex = models.CharField('性别',max_length=32, choices=gender, default='男')
    age = models.IntegerField('年龄')
    image = models.ImageField('照片',max_length=200,upload_to='user_images/%Y/%m/%d' ,default='default.png')
    content = models.CharField('简介',max_length=256)
    deleted = models.BooleanField(default=False)
    # school_id = models.ForeignKey('Schools.ID')
    sch_id = models.ForeignKey(Schools, to_field="school_id", on_delete=models.CASCADE)

    class Meta:
        db_table = 'teachers'
        verbose_name = '教师'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name


# 班级表
class Grades(models.Model):
    name = models.CharField('班级名称',max_length=128)
    grade_id = models.CharField(max_length=128, unique=True)
    boys_num = models.IntegerField('女生人数')
    girls_num = models.IntegerField('男生人数')
    content = models.CharField('班级介绍',max_length=256)
    class_teacher = models.ForeignKey(Teachers,on_delete=models.CASCADE)
    deleted = models.BooleanField(default=False)

    class Meta:
        db_table = 'grades'
        verbose_name = '班级'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name


# 学生表
class Students(models.Model):
    gender = (('male', '男'), ('female', '女'))
    name = models.CharField('姓名',max_length=128)
    student_id = models.CharField('学号',max_length=128, unique=True)
    sex = models.CharField('性别',max_length=32, choices=gender, default='男')
    age = models.IntegerField(verbose_name='年龄')
    image = models.ImageField('照片',max_length=200,upload_to='user_images/%Y/%m/%d' ,default='default.png')
    deleted = models.BooleanField(default=False)
    # grade_id = models.ForeignKey('Grades.ID')
    grade = models.ForeignKey(Grades, on_delete=models.CASCADE)

    def get_absolute_url(self):
        return reverse('AppStudent:student_detail',args=[str(self.student_id)])

    class Meta:
        db_table = 'students'
        verbose_name = '学生'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name


# 课程表
class Courses(models.Model):
    name = models.CharField('课程名称',max_length=128)
    course_id = models.CharField('课程编号',max_length=128, unique=True)
    deleted = models.BooleanField(default=False)

    class Meta:
        db_table = 'courses'
        verbose_name = '课程'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name


# 成绩表
class Scores(models.Model):
    # stu_id = models.CharField(max_length=128)
    # stu_id = models.ForeignKey('Students.ID')
    # cou_id = models.CharField(max_length=128)
    # cou_id = models.ForeignKey('Courses.ID')

    stu_id = models.ForeignKey(Students, to_field="student_id", on_delete=models.CASCADE)
    cou_id = models.ForeignKey(Courses, to_field="course_id", on_delete=models.CASCADE)
    score = models.FloatField('分数')

    class Meta:
        db_table = 'scores'
        verbose_name = '成绩'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

